package com.yf.exam.modules.tmpl.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yf.base.api.exception.ServiceException;
import com.yf.base.utils.BeanMapper;
import com.yf.base.utils.DecimalUtils;
import com.yf.exam.modules.tmpl.dto.ext.TmplQuAnswerDetailDTO;
import com.yf.exam.modules.tmpl.entity.TmplQuAnswer;
import com.yf.exam.modules.tmpl.mapper.TmplQuAnswerMapper;
import com.yf.exam.modules.tmpl.service.TmplQuAnswerService;
import com.yf.repo.enums.QuType;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
* <p>
* 试卷题目细则业务实现类
* </p>
*
* @author 聪明笨狗
* @since 2021-04-25 13:59
*/
@Service
public class TmplQuAnswerServiceImpl extends ServiceImpl<TmplQuAnswerMapper, TmplQuAnswer> implements TmplQuAnswerService {

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void saveAll(String refId, String quType, BigDecimal totalScore, Boolean pathScore, String content,
                        List<TmplQuAnswerDetailDTO> answerList) {

        if(CollectionUtils.isEmpty(answerList)){
            return;
        }

        // 部分给分判断
        if(pathScore != null && pathScore) {
            boolean check = this.checkScore(quType, totalScore, answerList);
            if (!check) {
                throw new ServiceException("题目：" + content + " 部分给分溢出！");
            }
        }

        List<TmplQuAnswer> list = new ArrayList<>();
        for(TmplQuAnswerDetailDTO item: answerList){
            TmplQuAnswer answer = new TmplQuAnswer();
            BeanMapper.copy(item, answer);
            answer.setRefId(refId);
            // 清空部分给分逻辑
            if(pathScore==null || !pathScore){
                answer.setPathScore(DecimalUtils.zero());
            }

            list.add(answer);
        }



        this.saveOrUpdateBatch(list);
    }

    @Override
    public void removeByRefIds(List<String> refIds) {
        QueryWrapper<TmplQuAnswer> wrapper = new QueryWrapper<>();
        wrapper.lambda().in(TmplQuAnswer::getRefId, refIds);
        this.remove(wrapper);
    }


    /**
     * 校验分数是否正确
     * @param totalScore
     * @param list
     * @return
     */
    private boolean checkScore(String quType, BigDecimal totalScore, List<TmplQuAnswerDetailDTO> list){


        // 校验单选题
        if(QuType.RADIO.equals(quType)){
            return this.checkRadio(totalScore, list);
        }

        // 先获全部取正确的选项
        List<String> rightList = new ArrayList<>();
        for(TmplQuAnswerDetailDTO item: list){
            if(item.getIsRight()) {
                rightList.add(item.getAnswerId());
            }
        }


        for(String qu: rightList){
            BigDecimal total = DecimalUtils.zero();
            for(TmplQuAnswerDetailDTO item: list){
                if(item.getPathScore()!=null
                        && DecimalUtils.gt(item.getPathScore(), 0)
                        && !qu.equals(item.getAnswerId())){

                    total = DecimalUtils.add(total, item.getPathScore());
                }
            }

            if(DecimalUtils.gt(total, totalScore)){
                return false;
            }
        }

        return true;

    }


    /**
     * 校验单选题
     * @param totalScore
     * @param list
     * @return
     */
    private boolean checkRadio(BigDecimal totalScore, List<TmplQuAnswerDetailDTO> list){

        for(TmplQuAnswerDetailDTO item: list){
            if(item.getPathScore()!=null
                    && DecimalUtils.gt(item.getPathScore(), 0)){
                if(DecimalUtils.gt(item.getPathScore() , totalScore)){
                    return false;
                }
            }
        }

        return true;
    }
}
